package co.wizardattack.connection;

import protocol.OnNewMessageListener;
import protocol.PairedConnection;
import protocol.Print;

public class MessageDistpacher implements OnNewMessageListener {

	public static final MessageDistpacher INSTANCE = new MessageDistpacher();
	private OnNewMessageListener listener;
	private PairedConnection pairedConnection;
	private MessageDistpacher() {}

	public void setListener(OnNewMessageListener listener) {
		Print.print("MessageDistpacher-setListener("+listener.getClass()+")");
		this.listener = listener;
		if(pairedConnection != null) {
			this.listener.getPairedConnection(pairedConnection);
			Print.print("Seting paired connection");
		}
	}

	@Override
	public int onNewMessage(String message) {
//		Print.print("wizardgame", message);
		// Comentario
		if(listener != null)
			return listener.onNewMessage(message);
		return -1;
	}

	@Override
	public void getPairedConnection(PairedConnection connection) {
		this.pairedConnection = connection;
		if(listener != null)
			listener.getPairedConnection(connection);
//		Print.print(listener == null ? "listener es null pero yo guardo la instancia" : "listener no es null");
	}

	@Override
	public void onCloseConnection() {
		this.pairedConnection = null;
		if(listener != null)
			listener.onCloseConnection();
		Print.print("La conexion se ha cerrado");
	}

}
